//
//  OMCouponSelectTableCell.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2016/12/29.
//  Copyright © 2016年 Kamfat. All rights reserved.
//

#import "OMCouponSelectTableCell.h"
#import "OMCheapModel.h"
#import <YYText.h>
#import "XZLCustomTextAttachment.h"
@interface OMCouponSelectTableCell ()
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *accessLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *expiredLabel;
@property (nonatomic, strong) UIView *rightContainView;
@property (nonatomic, strong) UIImageView *tagIV;
@property (nonatomic, strong) XZLCustomTextAttachment *attachment;
@end

@implementation OMCouponSelectTableCell
{
    UIImage *_enableImage;
    UIImage *_disableImage;
    UIImage *_expiredImage;
    UIImage *_usedImage;
}

- (void)configure
{
    [super configure];
    self.backgroundColor = nil;
    self.contentView.backgroundColor = nil;

    _attachment = [[XZLCustomTextAttachment alloc]init];
    _attachment.image = [UIImage imageNamed:@"ic_coupon_expire_tag_36_15_"];
    _attachment.height = SM_SIZE(30);

    _enableImage    = [UIImage imageNamed:@"ic_coupon_bg_red_125_100_"];
    _disableImage   = [UIImage imageNamed:@"ic_coupon_bg_disable_125_100_"];
    _expiredImage   = [UIImage imageNamed:@"ic_coupon_expired_68_70_"];
    _usedImage      = [UIImage imageNamed:@"ic_coupon_used_68_70_"];

    [self.iconImageView addSubview:self.priceLabel];
    [self.iconImageView addSubview:self.accessLabel];
    [self.contentView addSubview:self.rightContainView];
    [self.contentView addSubview:self.iconImageView];
    [self.rightContainView addSubview:self.titleLabel];
    [self.rightContainView addSubview:self.expiredLabel];
    [self.rightContainView addSubview:self.tagIV];
    [self setupConstraints];
}

- (void)configureCellWithObj:(OMCheapModel *)coupon
{

    NSMutableAttributedString *attributedPrice = [[NSMutableAttributedString alloc]initWithString:@"￥"];
    [attributedPrice appendAttributedString:[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",@(coupon.price)] attributes:@{NSFontAttributeName:SM_NBFont(110)}]];
    self.priceLabel.attributedText = attributedPrice;
    self.accessLabel.text = coupon.minimum_price?[NSString stringWithFormat:@"满%@元可用",@(coupon.minimum_price)]:@"无金额限制";// coupon.desc;
    self.titleLabel.text = coupon.name;

    //有效期
    NSMutableAttributedString *expired = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@" %@ - %@",coupon.begin_date,coupon.end_date]];
    expired.yy_font = SM_NFont(24);
    expired.yy_color = SM_lightGrayColor;
    [expired insertAttributedString:[NSAttributedString attributedStringWithAttachment:_attachment] atIndex:0];
    expired.yy_lineSpacing = SM_SIZE(5);
    self.expiredLabel.attributedText = expired;

    //图标
    UIImage *icon = _enableImage;
    UIImage *tagIcon = nil;
    if (coupon.is_used || coupon.has_expired) {
        icon = _disableImage;
        if (coupon.has_expired) {
            tagIcon = _expiredImage;
        }
        if (coupon.is_used) {
            tagIcon = _usedImage;
        }
    }
    self.tagIV.image = tagIcon;
    self.iconImageView.image = icon;
}

- (void)setupConstraints
{
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(20)));
        make.top.bottom.equalTo(@(SM_SIZE(0)));
        make.width.equalTo(self.iconImageView.mas_height).multipliedBy(1.2);
    }];

    [self.rightContainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(-2);
        make.right.equalTo(@(SM_SIZE(-20)));
        make.top.bottom.equalTo(@(0));
    }];

    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconImageView.mas_centerX);
        make.centerY.equalTo(self.iconImageView.mas_centerY).offset(SM_SIZE(-25));
    }];

    [self.accessLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconImageView.mas_centerX);
        make.top.equalTo(self.priceLabel.mas_bottom).offset(SM_SIZE(10));
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(30)));
        make.bottom.equalTo(self.rightContainView.mas_centerY).offset(SM_SIZE(-20));
        make.right.equalTo(@(SM_SIZE(-30)));
    }];

    [self.expiredLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.rightContainView.mas_centerY).offset(SM_SIZE(10));
        make.right.equalTo(self.titleLabel.mas_right);
    }];

    [self.tagIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(@0);
    }];

}

#pragma mark - Getters

- (UIImageView *)tagIV
{
    if (!_tagIV) {
        _tagIV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_coupon_expired_68_70_"]];
//        @"ic_coupon_used_68_70_"
    }
    return _tagIV;
}

- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_coupon_bg_red_125_100_"]];
    }
    return _iconImageView;
}

- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [UILabel new];
        _priceLabel.font = SM_NFont(45);
        _priceLabel.textColor = SM_whiteColor;
    }
    return _priceLabel;
}

- (UILabel *)accessLabel
{
    if (!_accessLabel) {
        _accessLabel = [UILabel new];
        _accessLabel.font = SM_CFont(25);
        _accessLabel.textColor = SM_whiteColor;
    }
    return _accessLabel;
}


- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = SM_CFont(30);
        _titleLabel.numberOfLines = 2;
        _titleLabel.textColor = SM_grayColor;
    }
    return _titleLabel;
}

- (UILabel *)expiredLabel
{
    if (!_expiredLabel) {
        _expiredLabel = [UILabel new];
        _expiredLabel.font = SM_NFont(24);
        _expiredLabel.numberOfLines = 2;
        _expiredLabel.textColor = SM_lightGrayColor;
    }
    return _expiredLabel;
}

- (UIView *)rightContainView
{
    if (!_rightContainView) {
        _rightContainView = [UIView new];
        _rightContainView.backgroundColor = SM_whiteColor;
        _rightContainView.layer.borderColor = SM_placeholdColor.CGColor;
        _rightContainView.layer.borderWidth = 1.f;
    }
    return _rightContainView;
}






+ (CGFloat)cellHeight
{
    return SM_SIZE(180);
}

@end
